from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.html import format_html
from rest_framework.authtoken.models import Token


# 继承django的user表
class User(AbstractUser):
    id = models.BigAutoField(primary_key=True)
    phone = models.CharField(max_length=11, blank=True, null=False)
    fullname = models.CharField(max_length=30, blank=True, null=False)


class AlbumStyle(models.Model):
    """相册风格表"""
    id = models.BigAutoField(primary_key=True)
    style_name = models.CharField(max_length=50, verbose_name="相册风格")

    def __str__(self):
        return self.style_name

    class Meta:
        verbose_name_plural = '相册风格'
        verbose_name = '风格'


class Album(models.Model):
    """相册"""
    id = models.BigAutoField(primary_key=True)
    album_name = models.CharField('相册名称', max_length=50)
    album_style = models.ForeignKey(AlbumStyle, related_name='Album_Style', on_delete=models.PROTECT,
                                    verbose_name="相册风格")
    Description = models.CharField('相册描述', max_length=500)
    owner = models.ForeignKey(User, related_name='Album_User', on_delete=models.CASCADE, verbose_name="所属用户")
    add_time = models.DateTimeField(verbose_name='相册创建时间', auto_now_add=True)
    update_time = models.DateTimeField('相册修改时间', auto_now=True)

    def __str__(self):
        return self.album_name

    def operate_str(self):
        parameter_str = 'album_id={}'.format(str(self.id))
        color_code = ''
        btn_str = '<button ><a href="{}"><span class="am-icon-trash-o"></span> 查看</a></button>'
        return format_html(btn_str, '/picture/by_albumid/?{}'.format(parameter_str))

    operate_str.short_description = '操作'

    class Meta:
        verbose_name_plural = '相册列表'
        verbose_name = '相册'


class Picture(models.Model):
    """照片列表"""
    id = models.BigAutoField(primary_key=True)
    picture_name = models.CharField('图片名称', max_length=50)
    picture_url = models.CharField('图片url', max_length=500)
    add_time = models.DateTimeField(verbose_name='图片上传时间', auto_now_add=True)
    status = models.CharField('是否删除', max_length=1)
    album_id = models.ForeignKey(Album, on_delete=models.PROTECT, verbose_name="所属相册")

    def __str__(self):
        return self.picture_name

    class Meta:
        verbose_name_plural = '照片列表'
        verbose_name = '照片'
